<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Db;
use think\Model;

class DreamComments extends Model{
    protected $autoWriteTimestamp = false;

    /**
     * @param int $id
     * @param string $content
     * @param int $uid
     * @param int $flow_id
     * @param string $out
     * @return bool
     */
    public function comments($id=0,$content='',$uid=0,$flow_id=0,&$out=''){
        $info = (new Dream())->find($id);
        if(!$info){
            $out = '内容已不存在';
            return false;
        }
        $data = [
            'aid'=>$id,
            'content'=>$content,
            'uid'=>$uid,
            'flow_id'=>$flow_id,
            'status'=> 2,
            'create_time'=>time(),
        ];
        $valid = new \app\first\validate\Comments();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        Db::startTrans();
        $id = $this->insertGetId($data);
        if($id){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }
    }

    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='create_time desc',$limit=10,$filed='*'){
        $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,avatar');
            }])
            ->where($where)
            ->group('aid')
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    /**
     * @param array $where
     * @param string $order
     * @param string $filed
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAllList($where=[],$order='create_time desc',$filed='*'){
        $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,account,nickname,avatar');
            }])
            ->where($where)
            ->order($order)
            ->select();
        foreach ($list as &$v){
            if($v['flow_id']) {
                $v['flow_user'] = $this->getMemberByFlowId($v['flow_id']);
            }else{
                $v['flow_user'] = [];
            }
        }
        return $list;
    }

    /**
     * @param int $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMemberByFlowId($id=0){
        $uid = $this->getFieldById($id,'uid');
        return (new Member())->field('id,account,nickname,avatar')->find($uid);
    }

    public function user(){
        return $this->hasOne('member','id','uid');
    }

    public function dream(){
        return $this->hasOne('dream','id','aid')
            ->bind([
                'member_id' => 'id'
            ]);
    }

    public function at(){
        return $this->hasOne('member','id','member_id');
    }
    /**
     * 收藏
     * @param int $mid
     * @param int $uid
     * @param int $type
     * @param string $out
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function subscribe($mid=0,$uid=0,$type=1,&$out=''){
        if(!$mid || !$uid){
            $out = '缺少参数';
            return false;
        }
        $info = $this
            ->where('uid','eq',$mid)
            ->where('rid','eq',$uid)
            ->find();
        if($type==1){
            if($info){
                $out = '你已经粉丝了';
                return false;
            }
            if(!$this->save([
                'mid'=>$mid,
                'rid'=>$uid
            ])){
                $out = '关注失败';
                return false;
            }
            return true;
        }else{
            if(!$info){
                $out = '你不是粉丝';
                return false;
            }
            if(!$info->delete()){
                $out = '取消失败';
                return false;
            }
            return true;
        }
    }
}